package com.sans.linear;

import org.junit.Test;

import javax.naming.Name;
import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @author Sans
 */
public class ArrayTest {
    @Test
    public void oneArrayBuild() {
        int[] arrA = {7, 77, 777};
        System.out.println(arrA[0]);
        System.out.println(arrA[1]);
        System.out.println(arrA[2]);

        int[] arrB = new int[3];
        arrB[0] = 7;
        arrB[1] = 77;
        System.out.println(arrB[0]);
        System.out.println(arrB[1]);
        System.out.println(arrB[2]);

        int[] arrC = {7, 77, 777};
        System.out.println(arrC);

        char[] arrD = {'o', 'k'};
        System.out.println(arrD);
        System.out.println("char数组内存地址:" + arrD.toString());

        boolean[] arrE = new boolean[1];
        System.out.println(arrE[0]);
    }

    @Test
    public void oneArrayTraverse() {
        int[] arrA = {7, 77, 777};
        for (int i = 0, j = arrA.length; i < j; i++) {
            System.out.println(arrA[i]);
        }
        for (int e : arrA) {
            System.out.println(e);
        }
        System.out.println(Arrays.toString(arrA));
    }

    @Test
    public void towArrayBuild() {
        int[][] arrA = {
                {6, 66, 666},
                {7, 77, 777},
                {8, 88, 888}
        };
        System.out.println(Arrays.deepToString(arrA));

        int[][] arrB = new int[3][3];
        arrB[0][0] = 6;
        arrB[0][1] = 66;
        arrB[0][2] = 666;

        arrB[1][0] = 7;
        arrB[1][1] = 77;
        arrB[1][2] = 777;

        arrB[2][0] = 8;
        arrB[2][1] = 88;
        arrB[2][2] = 888;
        System.out.println(Arrays.deepToString(arrB));
    }

    @Test
    public void twoArrayTraverse() {
        int[][] arrA = {
                {6, 66, 666},
                {7, 77, 777},
                {8, 88, 888}
        };
        for (int i = 0, j = arrA.length; i < j; i++) {
            for (int m = 0, n = arrA[i].length; m < n; m++) {
                System.out.println(arrA[i][m] + "\t");
            }
            System.out.println();
        }

        for (int[] e : arrA) {
            for (int i : e) {
                System.out.println(i + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 求数组：{1, 3, 5, 7, 9, 2, 4, 6, 8, 0}，的最小值，最大值和平均值。
     */
    @Test
    public void test1() {
        int[] arrA = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0};
        int max = arrA[0], min = arrA[0], length = arrA.length;
        double sum = 0.0;
        for (int e : arrA) {
            if (max < arrA[e]) {
                max = arrA[e];
            }
            if (min > arrA[e]) {
                min = arrA[e];
            }
            sum += arrA[e];
        }
        System.out.println("最小值为:" + min);
        System.out.println("最大值为:" + max);
        System.out.println("平均值为:" + sum / length);
    }
}
